package com.hao.epidemic_prevention.security.handler;

import com.hao.epidemic_prevention.utils.JSONResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {

        // 设置响应状态
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        // 设置响应类型
        httpServletResponse.setContentType("application/json;charset=utf-8");

        JSONResult result = new JSONResult(403, "未授权！", null);
        httpServletResponse.getWriter().write(String.valueOf(result));
    }
}
